#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
$, = "\t"; $\ = "\n";
my $before_day=1; 
if($ARGV[0]> 1){
	$before_day=$ARGV[0];
}

	print "备份 [前$before_day天] 至 [当天] 的数据!";

	my @months = qw(01 02 03 04 05 06 07 08 09 10 11 12);
	(my $sec,my $min,my $hour,
		my $mday,my $mon,my $year,
		my $wday,my $yday,my $isdst) = localtime();
	$year = $year+1900;
	my $date = $year."-".$months[$mon]."-".$mday;
	print "当前日期: $date";

	(my $bsec,my $bmin,my $bhour,
		my $bmday,my $bmon,my $byear,
		my $bwday,my $byday,my $bisdst) = localtime(time()-86400*$before_day);
	$byear = $byear+1900;
	if($bmday<10){
		$bmday="0$bmday";
	}
	my $date_before = $byear."-".$months[$bmon]."-".$bmday;
	print "前$before_day天: $date_before";

	my $backup_latest_path = "\/mnt\/sdb\/backup\/latest\/mongodb";
	my $backup_full_path = "\/mnt\/sdb\/backup\/full\/mongodb";
	qx / mkdir -p $backup_latest_path /;
	qx / mkdir -p $backup_full_path /;

	# mongodump -h 127.0.0.1 -p 27017 -d ecg-shadow -c bizlog  -q '{"created":{"$gte":new Date("2019-07-04T00:00:00Z")}}'   -o /tmp/test 

	my $date_begin = $date_before."T00:00:00Z";

	print "生成 最近$before_day天 的 bizlog 备份：$backup_latest_path";
	qx/\/usr\/bin\/mongodump -h 127.0.0.1 -p 27017 -d ecg -c bizlog  -q '{"created":{"\$gte":new Date("$date_begin")}}' -o $backup_latest_path /;

	print "生成 最近$before_day天 的 ecgresult 备份：$backup_latest_path";
	qx/\/usr\/bin\/mongodump -h 127.0.0.1 -p 27017 -d ecg -c ecgresult  -q '{"created":{"\$gte":new Date("$date_begin")}}' -o $backup_latest_path /;
